﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: Login.cs
// 作 者：hexingxiang
// 日 期：2011/09/16
// 描 述：用户登陆管理类
// 版 本：2.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 2.00   2011-10-24   hexingxiang      
//==================================================================
using System;
using System.Web.UI;
using System.Web.Security;
using ZX.BLL;
using ZX.EHR.Library;
using ZX.Common;
using ZX.BLL.Extend;
using ZX.DBModule;

namespace ZX.Web.UI.Controls
{
    /// <summary>
    /// 用户登陆管理类
    /// </summary>
    public partial class Login : ZX.Web.UI.Controls.BasePage
    {
        public string validateCode = System.Configuration.ConfigurationManager.AppSettings["validateCode"];
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetStringFromQueryString("action","") == "login")
            {
              string UserName =  Request.Form["txtUserName"].ToString();
              string PassWord =  Request.Form["txtPassword"].ToString();
              string Code = Request.Form["txtCode"].ToString();
              UserLogin(UserName, PassWord, Code);
              Response.End();
            }
            else
            {
                string cf = Request.QueryString["Comefrom"];//从移动端跳转过来传递的参数
                /*
                if (UserPoint() && cf == null)
                {
                    Response.Redirect("Mobi/default.htm");
                }
                */
                Random random = new Random();
                this.vimg.Src = "VCode.aspx?time" + random.Next();
            }
        }
        /// <summary>
        /// 登录按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            FormsAuthentication.SignOut();
            Session["User"] = null;
            Session["IsOutSite"] = null;
            UserLogin();
        }

        private void UserLogin(string UserName, string PassWord, string Code)
        {
            BUser buser = new BUser();
            DatUser dtusr = new DatUser();
            dtusr.LoginName = UserName.Trim();
            dtusr.Password = ExtendMethods.GetMd5Hash(PassWord);
            if (validateCode == "none")
            {
                UserLogin(buser, dtusr);
            }
            else
            {
                if (Session["vcode"] != null)
                {
                    if (Session["vcode"].ToString().ToUpper() !=Code.ToUpper())
                    {
                        this.Response.Write("3");
                        //ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "", "javascript:LoadVCode(0);", true);
                    }
                    else
                    {
                        UserLogin(buser, dtusr);
                    }
                }
                else
                {
                    this.Response.Write("3");
                    //ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "msg", "javascript:LoadVCode(0);", true);
                }
            }
        }


        private void UserLogin()
        {
            BUser buser = new BUser();
            DatUser dtusr = new DatUser();
            dtusr.LoginName = this.txtUserName.Value.Trim();
            dtusr.Password = ExtendMethods.GetMd5Hash(this.txtPassword.Value.Trim());
            if (validateCode == "none")
            {
                UserLogin(buser, dtusr);
            }
            else
            {
                if (Session["vcode"] != null)
                {
                    if (Session["vcode"].ToString().ToUpper() != this.txtCode.Value.Trim().ToUpper())
                    {
                        this.Response.Write("3");
                        //ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "", "javascript:LoadVCode(0);", true);
                    }
                    else
                    {
                        UserLogin(buser, dtusr);
                    }
                }
                else
                {
                    this.Response.Write("3");
                    //ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "msg", "javascript:LoadVCode(0);", true);
                }
            }
        }


        private void UserLogin(BUser buser, DatUser dtusr)
        {
            //buser.entity = dtusr;
            //buser.IsExist(dtusr);
            DatUser user = buser.GetDatUserByLoginNameAndPwd(dtusr);
            if (user == null)
            {
                this.Response.Write("0");
                //ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "msg", "javascript:LoadVCode(1);", true);
            }
            else
            {
                //用户登录时判断是否过期
                if (buser.IsExpire(dtusr))
                {
                    this.Response.Write("1");
                    //ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "msg", "javascript:LoadVCode(2);", true);
                }
                else
                {
                    WriteLog(user);
                    FormsAuthentication.SetAuthCookie(user.LoginName, true);
                    Session["User"] = user;
                    Session["IsOutSite"] = user.IsOutsite;
                    this.Response.Write("2");
                }
            }
        }

     
     
        /// <summary>
        /// 写登录日志
        /// </summary>
        private void WriteLog(DatUser user)
        {
            //base.WriteExpLog();

            BaseBussnies buss = new BaseBussnies();
            SysDataLog log = new SysDataLog();
            log.UserID = user.UserID;
            log.Action = "Login";
            log.Data = "IP:" + this.Request.UserHostAddress + ",LoginName:" + user.LoginName;
            log.EntityName = "DatUser";
            log.LogTime = DateTime.Now;
            buss.entity = log;
            buss.Insert();
        }
        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, ImageClickEventArgs e)
        {
            this.txtUserName.Value = "";
            this.txtPassword.Value = "";
            this.txtCode.Value = "";
        }
    }
}